﻿using System;
using System.Collections;
using System.IO;
using NVelocityTemplateEngine;
using NVelocityTemplateEngine.Interfaces;
using NUnit.Framework;

namespace Velocity.Test.NVelocityTemplateEngine
{
    [TestFixture]
    public class NVelocityFileEngineFixture
    {
        private INVelocityEngine engine;

        [SetUp]
        public void SetUp()
        {
            this.engine = NVelocityEngineFactory.CreateNVelocityFileEngine(
                Path.Combine(Environment.CurrentDirectory, "FileResources"),
                true);
        }

        [Test]
        public void SmokeTest()
        {
            Assert.IsNotNull(this.engine);
        }

        [Test]
        public void NotExistsDirTest()
        {
            var fault_engine = NVelocityEngineFactory.CreateNVelocityFileEngine(
                "not_exists_dir",
                true);

            var expected = "Unable to find resource 'not_exists_file'";

            Assert.AreEqual(expected, fault_engine.Process(new Hashtable(), "not_exists_file"));
        }

        [Test]
        public void NotExistsResourceInDirTest()
        {
            var fault_engine = NVelocityEngineFactory.CreateNVelocityFileEngine(
                Path.Combine(Environment.CurrentDirectory, "FileResources"),
                true);

            var expected = "Unable to find resource 'not_exists_file'";

            Assert.AreEqual(expected, fault_engine.Process(new Hashtable(), "not_exists_file"));
        }

        [Test]
        public void SimpleTest()
        {
            DateTime now = DateTime.Now;

            IDictionary context = new Hashtable();
            context["name"] = "John";
            context["surname"] = "Smith";
            context["templateType"] = "hibernate_lib_style";
            context["date"] = now;

            var actual = this.engine.Process(context, "file.simple.template.vm");

            var expected = String.Format(
                Common.EXPECTED,
                now);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TemplateWithClassTest()
        {
            DateTime now = DateTime.Now;

            IDictionary context = new Hashtable();
            context["clazz"] = new Common.Struct("John", "Smith", "hibernate_lib_style", now);

            var actual = this.engine.Process(context, "file.class.template.vm");

            var expected = String.Format(
                Common.EXPECTED,
                now);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TemplateWithArrayTest()
        {
            DateTime now = DateTime.Now;

            IDictionary context = new Hashtable();
            context["string_array"] = new object[] { "John", "Smith", "hibernate_lib_style", now };

            var actual = this.engine.Process(context, "file.array.template.vm");

            var expected = String.Format(
                Common.ARRAY_TEMPLATE_EXPECTED,
                now);

            Assert.AreEqual(expected, actual);
        }
    }
}
